/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.my.core.util;


import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 通用工具类
 *
 * @author Chill
 */
public class CommonUtil {
    /**
     * 表情字符过滤
     *
     * @param s
     * @return
     */
    public static String removeNonBmpUnicodes(String s) {
        return null == s ? null : s.replaceAll("[^\\u0000-\\uFFFF]", "");
    }

    public static Map<String, Date> getCurrentMonthFirstToEndAndDate(Date date) {
        Map<String, Date> m = new HashMap();
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 获取当月第一天
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MONTH, 0);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            String firstday = format.format(calendar.getTime());
            m.put("firstday", format2.parse(firstday + " 00:00:00"));
            // 获取当月最后一天
            calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MONTH, 1);
            calendar.set(Calendar.DAY_OF_MONTH, 0);
            String lastday = format.format(calendar.getTime());
            m.put("lastday", format2.parse(lastday + " 23:59:59"));
            //        // 打印结果字符串
//            System.out.println("本月第一天和最后一天分别是：" + firstday + " 和 " + lastday + "。");
            //        m.put("firstday",firstday);
            //        m.put("lastday",lastday);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return m;
    }

    /**
     * 当前月第一天到最后一天
     *
     * @return
     */
    public static Map<String, Date> getCurrentMonthFirstToEnd() {
        Map<String, Date> m = new HashMap();
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 获取当月第一天
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH, 0);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            String firstday = format.format(calendar.getTime());
            m.put("firstday", format2.parse(firstday + " 00:00:00"));
            // 获取当月最后一天
            calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH, 1);
            calendar.set(Calendar.DAY_OF_MONTH, 0);
            String lastday = format.format(calendar.getTime());
            m.put("lastday", format2.parse(lastday + " 23:59:59"));
            //        // 打印结果字符串
//            System.out.println("本月第一天和最后一天分别是：" + firstday + " 和 " + lastday + "。");
            //        m.put("firstday",firstday);
            //        m.put("lastday",lastday);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return m;
    }

    /**
     * 昨日时间段
     *
     * @return
     */
    public static Map<String, Date> getYesterday() {
        Map<String, Date> m = new HashMap();
        try {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            Calendar calendar = Calendar.getInstance();
//            calendar.set(Calendar.DATE, -1);
//            String yesterdayDate = dateFormat.format(calendar.getTime());
            calendar.set(Calendar.HOUR_OF_DAY, -24);
            String yesterdayDate = dateFormat.format(calendar.getTime());

            m.put("first", format2.parse(yesterdayDate + " 00:00:00"));
            m.put("last", format2.parse(yesterdayDate + " 23:59:59"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return m;
    }

    /**
     * 查找元素在第几位
     *
     * @param list
     * @param a    需要查找的元素
     * @param b    需要查找第几个元素
     * @return 返回元素在数组中的位置，用list[i]可以拿到
     */
    public static int getStringnum(String[] list, String a, int b) {
        if (list.length > 0 && b > 0) {
            for (int i = 0; i < list.length; i++) {
                if (list[i].equals(a)) {
                    b--;
                }
                if (b == 0) {
                    return i;
                }
            }
        }
        return -1;
    }

    /**
     * 查找元素在第几位
     *
     * @param list
     * @param a    需要查找的元素
     * @param b    需要查找第几个元素
     * @return 返回元素在数组中的位置，用list[i]可以拿到
     */
    public static int getLongnum(Long[] list, Long a, int b) {
        if (list.length > 0 && b > 0) {
            for (int i = 0; i < list.length; i++) {
                if (list[i].equals(a)) {
                    b--;
                }
                if (b == 0) {
                    return i;
                }
            }
        }
        return -1;
    }

    /**
     * 提取List对象属性转化为list<map> 用于返回值定制
     *
     * @param list
     * @param names
     * @param <T>
     * @return
     */
    public static <T> List<Map<Object, Object>> EntityConvertMap(List<T> list, List<String> names) {
        List<Map<Object, Object>> l = new LinkedList<>();
        try {
            for (T t : list) {
                Map<Object, Object> map = new HashMap<>();
                Method[] methods = t.getClass().getMethods();
                for (Method method : methods) {
                    String f = method.getName().substring(3);
                    if (method.getName().startsWith("get") && names.contains(f)) {
                        String name = method.getName().substring(3);
                        name = name.substring(0, 1).toLowerCase() + name.substring(1);
                        Object value = method.invoke(t);
//                        if(method.getGenericReturnType().toString().equals("class java.util.Date")&&value!=null){
//                            value = getNow((Date)value, "yyyy-MM-dd HH:mm:ss");
//                        }
                        map.put(name, value);
                    }
                }
                if (!map.isEmpty()) {
                    l.add(map);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return l;
    }

    /**
     * 验证手机号码
     *
     * @param mobiles
     * @return
     */
    public static boolean isMobileNO(String mobiles) {
        boolean flag = false;
//        String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}$";
        try {
            if (mobiles.length() == 11) {
                return true;
            }
//            Pattern p = Pattern.compile(regex);
//            Matcher m = p.matcher(mobiles);
//            flag = m.matches();
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }
}

